package com.hit.aircraftwar.strategy;


import com.hit.aircraftwar.aircraft.AbstractAircraft;
import com.hit.aircraftwar.aircraft.HeroAircraft;
import com.hit.aircraftwar.bullet.AbstractBullet;
import com.hit.aircraftwar.bullet.EnemyBullet;
import com.hit.aircraftwar.bullet.HeroBullet;
import com.hit.aircraftwar.bullet.Missile;
import com.hit.aircraftwar.bullet.TrackBullet;
import com.hit.aircraftwar.setter.ParameterSet;

import java.util.LinkedList;
import java.util.List;

/**
 * @author LiuYuZhe
 */
public class SpreadShootStrategy implements ShootStrategy {

    @Override
    public List<AbstractBullet> shoot(AbstractAircraft abstractAircraft) {

        int shootNum = abstractAircraft.getShootNum();
        int direction = abstractAircraft.getDirection();
        int power = abstractAircraft.getPower();

        List<AbstractBullet> res = new LinkedList<>();

        int x = abstractAircraft.getLocationX();
        int y = abstractAircraft.getLocationY() + direction*2;
        int speedOverAll = direction*24;
        double bulletGapAngle = Math.PI/180 * 10;
        AbstractBullet abstractBullet;

        if(abstractAircraft instanceof HeroAircraft) {
            int deviation;
            double bulletAngle = Math.PI/2 - bulletGapAngle * (shootNum - 1) / 2;
            for(int i=0; i<shootNum; i++) {
                if(shootNum%2 == 0 && i == shootNum/2) {
                    deviation = -10;
                } else {
                    deviation = 0;
                }
                switch(ParameterSet.INSTANCE.getHeroType()) {
                    case 1:
                        abstractBullet = new HeroBullet(x, y + deviation,
                                (int)(speedOverAll*Math.cos(bulletAngle)),
                                (int)(speedOverAll*Math.sin(bulletAngle)),
                                power);
                        break;
                    case 2:
                        abstractBullet = new Missile(x, y + deviation,
                                (int)(speedOverAll*Math.cos(bulletAngle)),
                                (int)(speedOverAll*Math.sin(bulletAngle)),
                                power);
                        break;
                    default:
                        abstractBullet = null;
                }
                res.add(abstractBullet);
                bulletAngle += bulletGapAngle;
            }
            //追踪子弹的添加
            if(ParameterSet.INSTANCE.isTrackBulletActive()) {
                for(int i = 0; i < 3; ++i) {
                    res.add(new TrackBullet(x + (i*2 - 3 + 1)*50, y, 0, direction*24, power));
                }
            }
        } else {
            bulletGapAngle *= 3;
            double bulletAngle = Math.PI/2 - bulletGapAngle * (shootNum - 1) / 2;
            for(int i=0; i<shootNum; i++) {
                abstractBullet = new EnemyBullet(
                        x, y,
                        (int)(speedOverAll*Math.cos(bulletAngle)),
                        (int)(speedOverAll*Math.sin(bulletAngle)),
                        power
                );
                res.add(abstractBullet);
                bulletAngle += bulletGapAngle;
            }
        }
        return res;
    }

}
